﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Sita.MainModule.Services;
using Sita.Entities;
using Sita.Presentation.ViewModels;

namespace Sita.Presentation.Controllers
{
    public class ReplacementBrandController : DefaultController
    {
        ReplacementBrandService replacementBrandService;

        public ReplacementBrandController()
        {
            replacementBrandService = new ReplacementBrandService();
        }

        public ActionResult Index()
        {
            return View();
        }

        public PartialViewResult List(int? index, string sortColumn, bool? sortAscending, int? pageSize)
        {
            int TotalRows;

            int CurrentBusinessUnitID = this.AuthenticatedUser.Business[this.AuthenticatedUser.CurrentBusinessUnitIndex].BusinessUnitID;

            if (string.IsNullOrEmpty(sortColumn))
            {
                sortColumn = "BrandName";
            }

            sortAscending = sortAscending ?? true;

            index = index ?? 0;

            if (!pageSize.HasValue)
            {
                pageSize = 5;
            }

            ViewData["PageSizeFilter"] = new SelectList(new int[] { 5, 10, 15, 20 });

            return PartialView(
                    new GenericGridList<ReplacementBrand>(
                        replacementBrandService.GetPaginatedReplacementBrandsByBussinesUnitID(CurrentBusinessUnitID, index.Value, pageSize.Value, sortColumn, sortAscending.Value, out TotalRows), index.Value, sortColumn, pageSize.Value, TotalRows, sortAscending.Value));
        }

        [HttpGet]
        public PartialViewResult Filter()
        {
            //IUserService UserService = IoCFactory.Resolve<IUserService>();
            //CommonController Common = new CommonController();
            //ViewData["PageSizeFilter"] = new SelectList(Common.GetPageSizes());

            return PartialView(new FilterModel() { PageSize = 5 });
        }

        #region CREATE
        [HttpGet]
        public PartialViewResult Create()
        {
            return PartialView("Create");
        }

        [HttpPost]
        public JsonResult Create(ReplacementBrand replacementBrand)
        {
            JsonResult Response;

            replacementBrand.BusinessUnitID = this.AuthenticatedUser.Business[this.AuthenticatedUser.CurrentBusinessUnitIndex].BusinessUnitID;

            replacementBrandService.AddReplacementBrand(replacementBrand);

            Response = new JsonResult();
            Response.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

            Response.Data = replacementBrand.BrandID > 0;

            return Json(Response);
        }
        #endregion

        #region EDIT
        [HttpGet]
        public PartialViewResult Edit(int? id)
        {
            if (id.HasValue)
            {
                ReplacementBrand selectedBrandReplacement = replacementBrandService.GetReplacementBrandByID(id.Value);
                return PartialView(selectedBrandReplacement);
            }
            else
            {
                return null;
            }
        }

        [HttpPost]
        public JsonResult Edit(ReplacementBrand replacementBrand)
        {
            ReplacementBrand OriginalReplacementBrand;
            JsonResult Response;

            OriginalReplacementBrand = replacementBrandService.GetReplacementBrandByID(replacementBrand.BrandID);

            Response = new JsonResult();
            Response.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

            if (OriginalReplacementBrand != null)
            {
                TryUpdateModel<ReplacementBrand>(OriginalReplacementBrand);

                replacementBrandService.ModifyReplacementBrand(OriginalReplacementBrand);

                Response.Data = true;
            }
            else
            {
                Response.Data = false;
            }

            return Json(Response);
        }

        #endregion

        public JsonResult Delete(int? id)
        {
            JsonResult JsonResponse;
            Entities.ReplacementBrand DeleteReplacementBrand;
            Dictionary<string, object> ResultDetails;

            ResultDetails = new Dictionary<string, object>();

            ResultDetails.Add("Message", string.Empty);
            ResultDetails.Add("Success", string.Empty);

            JsonResponse = new JsonResult();

            JsonResponse.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

            if (id.HasValue)
            {
                DeleteReplacementBrand = replacementBrandService.GetReplacementBrandByIDWithReplacements(id.Value);

                if (DeleteReplacementBrand != null)
                {
                    if (DeleteReplacementBrand.Replacements.Count > 0)
                    {
                        ResultDetails["Success"] = false;
                        ResultDetails["Message"] = Resources.ReplacementBrand.Index.ConfirmDeleteWithChildsMessage;
                    }
                    else
                    {
                        if (replacementBrandService.DeleteReplacementBrand(DeleteReplacementBrand))
                        {
                            ResultDetails["Success"] = true;
                        }
                        else
                        {
                            ResultDetails["Success"] = false;
                            ResultDetails["Message"] = Resources.ReplacementBrand.Index.CannotDelete;
                        }
                    }
                }
                else
                {
                    ResultDetails["Success"] = false;
                    ResultDetails["Message"] = Resources.ReplacementBrand.Index.InvalidReplacementBrand;
                }
            }
            else
            {
                ResultDetails["Success"] = false;
            }

            JsonResponse.Data = ResultDetails;

            return JsonResponse;
        }
    }
}
